import Vue from 'vue'
import VueRouter from 'vue-router'
import http from '@/axios/api'
// import el from 'element-ui/src/locale/lang/el'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/LoginView')
  },
  {
    path: '/essay',
    name: 'essay',
    component: () => import('@/views/MessageView')
  },
  {
    path: '/paper',
    name: 'paper',
    component: () => import('@/views/home/paperTemplate/paper')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/HomeView'),
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/home/index')
      },
      {
        path: '/wordPractice',
        name: 'wordPractice',
        component: () => import('@/views/home/wordPractice'),
        redirect: '/wordPractice/index',
        children: [
          {
            path: '/wordPractice/index',
            name: 'wordPracticeIndex',
            component: () => import('@/views/home/practice/index')
          },
          {
            path: '/wordPractice/record',
            name: 'wordPracticeRecord',
            component: () => import('@/views/home/practice/record')
          }
        ]
      },
      {
        path: '/translate',
        name: 'translate',
        component: () => import('@/views/home/translate')
      },
      {
        path: '/dictionaries',
        name: 'dictionaries',
        component: () => import('@/views/home/dictionaries')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  next(false)//不让走
  if (to.path != '/') {
    http.ver().then(res => {
      if (res.data.code == 400) {
        next({
          path: '/',
          params: { isVerFalse: true }
        })
      } else {
        next(true)
      }
    })
  } else {
    http.ver().then(res => {
      if (res.data.code == 200) {
        next('/home')
      } else {
        next(true)
      }
    })
  }
})
const originalPush = VueRouter.prototype.push

const originalReplace = VueRouter.prototype.replace

//push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

//replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject)
  }
  return originalReplace.call(this, location).catch(err => err)
}
export default router
